package engine.network
{
	import com.sociodox.utils.*;
	import engine.core.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	public class RxWebClient
	{
		private var m_url:String;
		private var m_loader:URLLoader;
		private var m_request:URLRequest;
		private var m_username:String;
		private var m_password:String;
		private var m_authorization:String;
		private var m_active:Boolean;
		private var m_httpStatus:int;
		private var m_callback:Function;
		
		public function RxWebClient(url:String)
		{
			this.m_url = url;
			this.m_active = false;
			this.m_authorization = null;
			this.m_request = new URLRequest();
			this.m_loader = new URLLoader();
			this.m_loader.addEventListener(Event.COMPLETE, this.OnComplete);
			this.m_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.OnHTTPStatus);
			this.m_loader.addEventListener(IOErrorEvent.IO_ERROR, this.OnIOError);
			this.m_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.OnSecurityError);
			Security.loadPolicyFile(this.m_url + "/crossdomain.xml");
		}
		
		private function OnComplete(event:Event) : void
		{
			var byte:ByteArray = null;
			var loader:* = URLLoader(event.currentTarget);
			if (loader.dataFormat == URLLoaderDataFormat.TEXT)
			{
				byte = new ByteArray();
				byte.writeUTFBytes(loader.data);
				byte.position = 0;
			}
			else if (loader.dataFormat == URLLoaderDataFormat.BINARY)
			{
				byte = loader.data;
			}
			else
			{
				RxLog.Error("RxWebClient.OnComplete(): Format not supported");
			}
			this.m_active = false;
			if (this.m_callback != null)
			{
				this.m_callback(this.m_httpStatus, byte);
			}
		}
		
		private function OnHTTPStatus(event:HTTPStatusEvent) : void
		{
			this.m_httpStatus = event.status;
		}
		
		private function OnIOError(event:IOErrorEvent) : void
		{
			this.m_active = false;
			if (this.m_callback != null)
			{
				this.m_callback(0, null);
			}
		}
		
		private function OnSecurityError(event:SecurityErrorEvent) : void
		{
			this.m_active = false;
			if (this.m_callback != null)
			{
				this.m_callback(0, null);
			}
		}
		
		public function SetAuthorization(username:String, password:String) : void
		{
			this.m_username = username;
			this.m_password = password;
			var byte:* = new ByteArray();
			byte.writeUTFBytes(username + ":" + password);
			this.m_authorization = Base64.encode(byte);
		}
		
		public function PostURLVariables(param1:String, param2:URLVariables, param3:Function) : void
		{
			this.Post(param1, null, param2, param3);
		}
		
		private function Post(param1:String, param2:String, param3:Object, param4:Function) : void
		{
			if (this.m_active)
			{
	
			}
			this.m_active = true;
			this.m_callback = param4;
			this.m_request.method = URLRequestMethod.POST;
			this.m_request.url = this.m_url + param1;
			if (param2 != null)
			{
				this.m_request.contentType = param2;
			}
			this.m_request.data = param3;
			if (this.m_authorization != null)
			{
				this.m_request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + this.m_authorization));
			}
			this.m_loader.load(this.m_request);
		}
		
		public function Delete() : void
		{
			if (this.m_active)
			{
				RxLog.Error("Attempting to delete an RxWebClient with an active request");
	
			}
			this.m_loader.removeEventListener(Event.COMPLETE, this.OnComplete);
			this.m_loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, this.OnHTTPStatus);
			this.m_loader.removeEventListener(IOErrorEvent.IO_ERROR, this.OnIOError);
			this.m_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.OnSecurityError);
			this.m_loader = null;
			this.m_request = null;
			this.m_authorization = null;

		}
		
		public function Get(param1:String, param2:String, param3:ByteArray, param4:Function) : void
		{
			if (this.m_active)
			{
				return;
			}
			this.m_active = true;
			this.m_callback = param4;
			this.m_request.method = URLRequestMethod.GET;
			this.m_request.url = this.m_url + param1;
			this.m_request.contentType = param2;
			this.m_request.data = param3;
			this.m_loader.load(this.m_request);
		}
		
		public function PostData(param1:String, param2:String, param3:ByteArray, param4:Function) : void
		{
			this.Post(param1, param2, param3, param4);
		}

	}
}